package com.scbgbm.iq.wxapi;

import android.text.TextUtils;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.fenghuajueli.libbasecoreui.constants.EventBusConstants;
import com.fenghuajueli.libbasecoreui.data.entity.eventbus.EventEntity;
import com.fenghuajueli.libbasecoreui.data.entity.goods.WxMiniPayResult;
import com.fenghuajueli.module_user.constants.PayConstant;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelbiz.WXLaunchMiniProgram;
import com.umeng.socialize.weixin.view.WXCallbackActivity;

import org.greenrobot.eventbus.EventBus;


public class WXEntryActivity extends WXCallbackActivity {

    @Override
    public void onResp(BaseResp resp) {

        if (resp.getType() == ConstantsAPI.COMMAND_LAUNCH_WX_MINIPROGRAM) {
            WXLaunchMiniProgram.Resp launchMiniProResp = (WXLaunchMiniProgram.Resp) resp;
            String extraData = launchMiniProResp.extMsg;
            try {
                if (!TextUtils.isEmpty(extraData)) {
                    WxMiniPayResult payResult = GsonUtils.fromJson(extraData, WxMiniPayResult.class);
                    switch (payResult.getCode()) {
                        case WxMiniPayResult.PAY_SUCCESS:
                            ToastUtils.showShort("支付成功");
                            PayConstant.goPay = false;
                            EventBus.getDefault().post(new EventEntity(EventBusConstants.PAY_SUCCESS, null));
                            break;
                        default:
                            //失败
                            ToastUtils.showShort("支付失败");
                            EventBus.getDefault().post(new EventEntity(EventBusConstants.PAY_ERROR, null));
                            break;
                    }
                } else {
                    PayConstant.pay = 0;
                    ToastUtils.showShort("支付失败");
                    EventBus.getDefault().post(new EventEntity(EventBusConstants.PAY_ERROR, null));
                }
            } catch (Exception e) {
                ToastUtils.showShort("支付失败");
                EventBus.getDefault().post(new EventEntity(EventBusConstants.PAY_ERROR, null));
            }
            finish();
        }
        super.onResp(resp);
    }
}
